﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myBoard.Framework.MVVM;
using System.ComponentModel;
using myBoard.ExternalAPI.Google.Translate;
using myBoard.Framework;

namespace myTwitter.BO
{
    public abstract class IMessageBase : ViewModelBase, IMessage
    {
        private string _text;
        private bool _isBusy;

        #region IMessage Members

        public virtual DateTime CreatedAt { get; set; }
        public virtual long ID { get; set; }
        public virtual User User { get; set; }
        public virtual string IsBusyText { get; set; }
        public virtual RelayCommand TranslateCommand { get; set; }

        public virtual string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                RaisePropertyChanged("Text");
            }
        }

        public virtual bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }

        public IMessageBase()
        {
            TranslateCommand = new RelayCommand(x => OnTranslate());
        }

        private void OnTranslate()
        {
            IsBusy = true;
            string from;
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += (a, b) =>
            {
                TranslateClient client = new TranslateClient("http://yannalet.com");
                b.Result = client.TranslateAndDetect(Text, System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName, out from);                
            };
            bw.RunWorkerCompleted += (a, b) =>
            {
                IsBusy = false;
                if (b.Error != null)
                {
                    ExceptionsManager.LogException("Sinapse", b.Error, true);
                }
                else
                {
                    Text = b.Result as string;
                }
            };
            bw.RunWorkerAsync();
        }

        #endregion
    }
}
